home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / src / GLperf3.12-src.lha / GLperf / TextFont.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-01  |  5.1 KB  |  167 lines

  1. /*
  2.  * (c) Copyright 1995, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  * Permission to use, copy, modify, and distribute this software for
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * US Government Users Restricted Rights
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * Author: John Spitzer, SGI Applied Engineering
  36.  *
  37.  */
  38.  
  39. #include "TextFont.h"
  40. #include "Global.h"
  41. #include "AttrName.h"
  42.  
  43. extern BitmapFontRec bitmap8By13;
  44. extern BitmapFontRec bitmap9By15;
  45. extern BitmapFontRec bitmapTimesRoman10;
  46. extern BitmapFontRec bitmapTimesRoman24;
  47. extern BitmapFontRec bitmapHelvetica10;
  48. extern BitmapFontRec bitmapHelvetica12;
  49. extern BitmapFontRec bitmapHelvetica18;
  50.  
  51. typedef struct _GLperfFont {
  52.     int name;
  53.     BitmapFontPtr fontPtr;
  54. } GLperfFont, *GLperfFontPtr;
  55.  
  56. static GLperfFont fonts[] = {
  57.     { f8x13, &bitmap8By13 },
  58.     { f9x15, &bitmap9By15 },
  59.     { timR10, &bitmapTimesRoman10 },
  60.     { timR24, &bitmapTimesRoman24 },
  61.     { helvR10, &bitmapHelvetica10 },
  62.     { helvR12, &bitmapHelvetica12 },
  63.     { helvR18, &bitmapHelvetica18 }
  64. };
  65.  
  66. TextFontPtr new_TextFont(int fontName)
  67. {
  68.     TextFontPtr this = (TextFontPtr)malloc(sizeof(TextFont));
  69.     int i;
  70.     BitmapFontPtr fontPtr;
  71.  
  72.     for (i = 0; i < sizeof(fonts) / sizeof(GLperfFont) && fonts[i].name != fontName; i++);
  73.     if (fonts[i].name != fontName) {
  74.     printf("GLperf: error in finding font\n");
  75.     exit(1);
  76.     }
  77.  
  78.     fontPtr = fonts[i].fontPtr;
  79.  
  80.     this->base = glGenLists((GLsizei)256);
  81.     if (this->base == 0) {
  82.         printf("GLperf: out of display lists\n");
  83.         exit(1);
  84.     }
  85.     
  86.     for (i = 0; i < fontPtr->first; i++) {
  87.     glNewList(this->base + i, GL_COMPILE);
  88.     /* Empty list */
  89.     glEndList();
  90.     }
  91.  
  92.     glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
  93.     glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
  94.     glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  95.     glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  96.     glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  97.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  98.  
  99.     for (i = fontPtr->first; i < fontPtr->first + fontPtr->num_chars; i++) {
  100.     BitmapCharPtr ch;
  101.  
  102.     glNewList(this->base + i, GL_COMPILE);
  103.     if (ch = fontPtr->ch[i - fontPtr->first]) {
  104.         glBitmap(ch->width, ch->height, (GLfloat)ch->xorig, (GLfloat)ch->yorig,
  105.                      (GLfloat)ch->advance, (GLfloat)0, ch->bitmap);
  106.     }
  107.     glEndList();
  108.     }
  109.  
  110.     for (i = fontPtr->first + fontPtr->num_chars; i < 256; i++) {
  111.     glNewList(this->base + i, GL_COMPILE);
  112.     /* Empty list */
  113.     glEndList();
  114.     }
  115.  
  116.     this->fontPtr = fontPtr;
  117.  
  118.     return this;
  119. }
  120.  
  121. void delete_TextFont(TextFontPtr this)
  122. {
  123.     glDeleteLists(this->base, 256);
  124.     free(this);
  125. }
  126.  
  127. GLuint TextFont__GetBase(TextFontPtr this)
  128. {
  129.     return this->base;
  130. }
  131.  
  132. void TextFont__StringSize(TextFontPtr this, char* string, GLint *width, GLint *widthPad, 
  133.                                         GLint *height, GLint *heightPad)
  134. {
  135.     char *p;
  136.     int leftmost = 9999;
  137.     int lowermost = 9999;
  138.     int rightmost = -9999;
  139.     int uppermost = -9999;
  140.     int x = 0;
  141.     int y = 0;
  142.     BitmapCharPtr ch;
  143.  
  144.     if (string == 0) {
  145.     *height = 0;
  146.         *heightPad = 0;
  147.         *width = 0;
  148.         *widthPad = 0;
  149.     return;
  150.     }
  151.  
  152.     for (p = string; *p; p++) {
  153.     if (ch = this->fontPtr->ch[*p]) {
  154.         leftmost = min(x - ch->xorig, leftmost);
  155.         lowermost = min(y - ch->yorig, lowermost);
  156.         rightmost = max(x - ch->xorig + ch->width, rightmost);
  157.         uppermost = max(y - ch->yorig + ch->height, uppermost);
  158.         x += ch->advance;
  159.     }
  160.     }
  161.  
  162.     *height = uppermost - lowermost;
  163.     *heightPad = lowermost;
  164.     *width = rightmost - leftmost + 1;
  165.     *widthPad = leftmost;
  166. }
  167.